/**
 * 
 */
package trungsi.gea.photos.client
import groovy.swing.SwingBuilder
import javax.swing.ImageIconimport javax.swing.JLabel
import javax.swing.JPanel
import java.awt.BorderLayout as BL
import javax.swing.JFileChooser as FC
import javax.swing.JFrame
import javax.imageio.ImageIO

class ImagePanel extends /*JComponent JPanel*/ JLabel {
    //def file
    //def url
    def imageLoader
    //def clickHandler
    
	def img
    
    /*ImagePanel() {}
    
    ImagePanel(File file) {
    	this.file = file
    	this.setLayout(null)
    	loadImageAsync()	
    }*/
    
    /*ImagePanel(URL url) {
    	this.url = url
    	loadImageAsync()
    }*/
    
	void setImageLoader(imageLoader) {
    	this.imageLoader = imageLoader
    	loadImageAsync()
    }
    
    void setOnClick(clickHandler) {
    	this.addMouseListener([mousePressed : {event -> 
    		clickHandler.call(event)
    	}] as java.awt.event.MouseAdapter)
    }
    
    private def loadImage() {
    	/*if (url) {
    		return ImageIO.read(url)
    	} 
    	ImageIO.read(file)*/
    	imageLoader.call()
    }
    
    void loadImageAsync() {
    	/*def worker = [doInBackground : {
    		img = loadImage()
        }, done : {
        	setPanelSize()
        }] as SwingWorker
        worker.execute()*/
    	img = loadImage()
    	//setPanelSize()
    	//def label = new JLabel(new ImageIcon(img))
    	//add(label)
    	this.setIcon(new ImageIcon(img))
    	
    }
    
    /*void scaleTo(value) {
    	img = loadImage()
    	img = img.getScaledInstance((img.getWidth(null)*value).toInteger(), -1, Image.SCALE_FAST)
    	setPanelSize()
    	//revalidate()
    	repaint()
    }*/
    
    /*void setPanelSize() {
    	def size = new Dimension(img.getWidth(null), img.getHeight(null))
        setPreferredSize(size)
        setMinimumSize(size)
        setMaximumSize(size)
        setSize(size)
        println "width = ${this.width},height = ${this.height}"
    }
    
    @Override
    public void paintComponent(Graphics g) {
    	super.paintComponent(g)
    	if (img != null) {
    		g.drawImage(img, 0, 0, null)
    	} 
    }*/
}
